package com.infinitegroup.findmystuff.activities;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.LoginPresenter;
import com.infinitegroup.findmystuff.views.LoginView;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class LoginActivity extends Activity implements LoginView{
	
	private LoginPresenter presenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		presenter = new LoginPresenter(this);
		
		Button login = (Button) findViewById(R.id.loginButton);
		Button register = (Button) findViewById(R.id.registerButton);
		
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onLoginClick();	
			}
		});
		
		register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onRegisterClick();
			}
		});
		
	}
	
	
	private void onLoginClick(){
		String username = ((EditText)findViewById(R.id.usernameText)).getText().toString();
		String password = ((EditText)findViewById(R.id.passwordText)).getText().toString();
		presenter.handleLoginClick(username, password);
	}
	
	private void onRegisterClick(){
		presenter.handleRegisterClick();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public void advanceToHome(String username) {
		Intent intent = new Intent(this, HomeActivity.class);
		intent.putExtra("username", username);
		startActivity(intent);
	}

	@Override
	public void retry() {
		Toast toast = Toast.makeText(this, "Invalid user name/password combination.", Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	@Override
	public void advanceToLocked() {
		Intent intent = new Intent(this, LockoutActivity.class);
		startActivity(intent);
		
	}

	@Override
	public void advanceToRegistration() {
		Intent intent = new Intent(this, RegistrationActivity.class);
		startActivity(intent);		
	}
	
	@Override
	public void displayMessage(String string) {
		Toast toast = Toast.makeText(this, string, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();		
	}

	@Override
	public void advanceToAdminHome(String adminUsername) {
		Intent intent = new Intent(this, AdminHomeActivity.class);
		intent.putExtra("username", adminUsername);
		startActivity(intent);
	}
	
	
/*
	// database test code.
public class LoginActivity extends Activity{
	private Item item;
	private Account account;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		item = new ItemLink(this);
		account = new AccountLink("", this);
		
		//account.register("Rohit", "Pathak", "rp", "pass", "rp@rp.com", "1234567890", "12345");
		
		//account.addItem("Car", "Delhi", "", "1000", "hello");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		//item.insertNewItem("Mazda2", "Atlanta", "", "2000", "hello", "lost", Item.CATEGORY_MISCELLANEOUS, "rp");
		//item.insertNewItem("Hat2", "Delhi", "", "200", "hola!", "found", Item.CATEGORY_MISCELLANEOUS, "rp");
		
		Button login = (Button) findViewById(R.id.loginButton);
		login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					List<Map<String, String>> items = item.getItemsFilteredBy(null, null, true);
					for (Map m : items){
						displayMessage(m.get("date") + " " + m.get("itemname") + " " +m.get("category") + " " +m.get("status"));
					}					
					//((TextView) findViewById(R.id.test_text)).setText(account.getFirstName() + " " + account.getEmail()+ " " + account.isActive());
				} catch (Exception e){
					e.printStackTrace();
					displayMessage("Errors and shit");
				}
				
			}
		});
	}
	
	public void displayMessage(String string) {
		Toast toast = Toast.makeText(this, string, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();		
	}
	*/
	

}
